#!/usr/bin/perl
# throughput per session
$event_filter = "[rs-]";
@ptype_filter = ("tcp", "ack", "cbr");

if (@ARGV < 1) {
    printf "usage hyun_tcp_tp.pl <trace file> [node1, ...]\n";
    exit;
}

$on_top = 1;
$hierarchical_addr = 0;

%packet_source = ();
%send_data = ();
%recv_data = ();
%total_send_data = ();
%total_recv_data = ();
%session_type = ();
%total_data = ();
$granularity = 1.0;

@p_time = ();

@session_list = ();
@trace_list = ();
@trace_type = ();

$tp_st_time = 10;  #Throughput Start Time
$tp_end_time = 30; #Throughput End Time
$tp_ = $tp_end_time - $tp_st_time;  

if ($ARGV[0] =~ /^-[sS]/) {
    $y_size = $ARGV[1];
    shift; shift;
}

$tracename = shift;
if ($tracename =~ /(.+)\.tr/) {
    $outname = $1 . ".png";
} else {
    $outname = "hyun_tcp_tp.png";
}


open(infile, $tracename) or die "couldn't open $tracename";
open(inspect_out, ">output.out") or die "couldn't open output.out";
while ($line = <infile>) {
    ++$line_no;

    next if ($line !~ /MAC/);
    next if ($line !~ /^$event_filter/);


    $cont = 0;
    foreach $t (@ptype_filter) {
        if ($line =~ /$t/) {
	    $cont = 1;
	    last;
	}
    }
    next unless ($cont);

    @entry = split(/[\s\(\)\[\]]+/, $line);
    $event = $entry[0];
    $time = $entry[1];

#    next if ($entry[1] < $tp_st_time);
#    next if ($entry[1] > $tp_end_time);

    # check for trace format and parse entry
    if ($entry[2] =~ /_+(\d+)_+/) { # mobile
	$type = $entry[6];
	$size = $entry[7];
	@src = split(/[:]/, $entry[13]);
	@dst = split(/[:]/, $entry[14]);
        #print "entry[2]: $entry[2]\n";
        @sender = split(/[_]/, $entry[2]);
        #print "sender[0]: $sender[0], sender[1]: $sender[1]\n";

        if ($event eq "s") {
            printf { inspect_out } "%d %.5f sending to %d originator %d\n", $sender[1], $time, $entry[16], $src[0];
        } elsif ($event eq "r") {
       #     printf { inspect_out } "%d %.5f received from %d originator %d\n", $sender[1], $time, $entry[16], $src[0];
        }

	#$pktid = $entry[5];
	$session = $src[0] . "." . $src[1] . "_" . $dst[0] . "." . $dst[1];
	#print "$session $time $event $size $type\n";

    } else {                        # standard
	$to = $entry[3];
	$pktid = $entry[11];

	if ($event ne "r") {
	    $packet_source{$pktid} = $to;
	    next;
	}

	$from = $entry[2];
	$type = $entry[4];
	$size = $entry[5];
	$packet_source{$pktid} = $to;
    }

}
# end of while 
close(infile);
